﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics.Contracts;

using System.ComponentModel.Composition;

namespace Kooboo.Commerce.Repositories
{
    [Export(typeof(IAccountRepository))]
    public partial class EntityRepositories:IAccountRepository
    {
        IQueryable<IAdministrator> IAccountRepository.Administrators
        {
            get
            {
                return this.DataReaderContext.Administrators;
            }
        }

        public void Post(IAdministrator administrator)
        {
            Contract.Requires(administrator != null);

            if (administrator == null)
            {
                throw new ArgumentNullException("Post(IAdministrator administrator)");
            }

            var admin = new Administrator();
            admin.UserName = administrator.UserName;

            this.DataWriterContext.Administrators.InsertOnSubmit(admin);
            this.DataWriterContext.SubmitChanges();

        }

        public void Delete(IAdministrator administrator)
        {
            if (administrator != null)
            {
                var find = this.DataWriterContext.Administrators
                    .Where(i => i.UserName == administrator.UserName)
                    .FirstOrDefault();

                if (find != null)
                {
                    this.DataWriterContext.Administrators.DeleteOnSubmit(find);
                    this.DataWriterContext.SubmitChanges();
                }
            }
        }

    }
}
